package com.yanqu.road.dao.impl.kowloon;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.union.RechargeUserInfo;
import com.yanqu.road.entity.kowloon.KowloonUserData;
import com.yanqu.road.entity.kowloon.sanxiao.KowloonGameData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KowloonUserDataDaoImpl extends TempDataObjectDao<KowloonUserData> {

    public static String getKowloonUserDataSql = "select * from `t_u_kowloon_user_data` where `userId`=?";

    public KowloonUserData getKowloonUserData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getKowloonUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateKowloonUserDataSql = "update `t_u_kowloon_user_data` set prosperousValue=?,chapterId=?,moraleLevel=?,moraleBroken=?,speedTimes=?,speedResetTime=?,incomeReward=?,lastSettlementRewardTime=?,lastGetRewardTime=?,guideBit=?,gameData=?,challengeTimes=?,challengeResetTime=?,updateTime=? where userId=?";

    @Override
    protected boolean updateData(KowloonUserData kowloonUserData) {
        kowloonUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, kowloonUserData.getProsperousValue()));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonUserData.getChapterId()));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonUserData.getMoraleLevel()));
        para.put(i++, new DbParameter(Types.BOOLEAN, kowloonUserData.isMoraleBroken()));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonUserData.getSpeedTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonUserData.getSpeedResetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, kowloonUserData.getIncomeReward()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonUserData.getLastSettlementRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonUserData.getLastGetRewardTime()));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonUserData.getGuideBit()));
        para.put(i++, new DbParameter(Types.NVARCHAR, kowloonUserData.getGameData() == null ? "" : JSON.toJSONString(kowloonUserData.getGameData())));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonUserData.getChallengeTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonUserData.getChallengeResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonUserData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonUserData.getUserId()));
        if (getDb().executeNoneQuery(updateKowloonUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertKowloonUserDataSql = "insert into `t_u_kowloon_user_data`(userId,prosperousValue,chapterId,moraleLevel,moraleBroken,speedTimes,speedResetTime,incomeReward,lastSettlementRewardTime,lastGetRewardTime,guideBit,gameData,challengeTimes,challengeResetTime,updateTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(KowloonUserData kowloonUserData) {
        kowloonUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, kowloonUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonUserData.getProsperousValue()));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonUserData.getChapterId()));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonUserData.getMoraleLevel()));
        para.put(i++, new DbParameter(Types.BOOLEAN, kowloonUserData.isMoraleBroken()));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonUserData.getSpeedTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonUserData.getSpeedResetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, kowloonUserData.getIncomeReward()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonUserData.getLastSettlementRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonUserData.getLastGetRewardTime()));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonUserData.getGuideBit()));
        para.put(i++, new DbParameter(Types.NVARCHAR, kowloonUserData.getGameData() == null ? "" : JSON.toJSONString(kowloonUserData.getGameData())));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonUserData.getChallengeTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonUserData.getChallengeResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonUserData.getUpdateTime()));
        if (getDb().executeNoneQuery(insertKowloonUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected KowloonUserData parseData(ResultSet rs) throws SQLException {
        KowloonUserData kowloonUserData = new KowloonUserData();
        kowloonUserData.setUserId(rs.getLong("userId"));
        kowloonUserData.setProsperousValue(rs.getLong("prosperousValue"));
        kowloonUserData.setChapterId(rs.getInt("chapterId"));
        kowloonUserData.setMoraleLevel(rs.getInt("moraleLevel"));
        kowloonUserData.setMoraleBroken(rs.getBoolean("moraleBroken"));
        kowloonUserData.setSpeedTimes(rs.getInt("speedTimes"));
        kowloonUserData.setSpeedResetTime(rs.getLong("speedResetTime"));
        kowloonUserData.setIncomeReward(rs.getString("incomeReward"));
        kowloonUserData.setLastSettlementRewardTime(rs.getLong("lastSettlementRewardTime"));
        kowloonUserData.setLastGetRewardTime(rs.getLong("lastGetRewardTime"));
        kowloonUserData.setGuideBit(rs.getInt("guideBit"));
        String gameData = rs.getString("gameData");
        if (!StringUtils.isNullOrEmpty(gameData)) {
            kowloonUserData.setGameData(JSON.parseObject(gameData, new TypeReference<KowloonGameData>() {
            }));
        }
        kowloonUserData.setChallengeTimes(rs.getInt("challengeTimes"));
        kowloonUserData.setChallengeResetTime(rs.getLong("challengeResetTime"));
        kowloonUserData.setUpdateTime(rs.getLong("updateTime"));
        return kowloonUserData;
    }


}
